<?php

/**
 * @author zhuang<pingguoli@gmail.com>
 * @Date 2020/7/7 上午8:28
 *
 * @copyright Copyright 2020 - 2021
 */

namespace App\Http\Controllers\Api\Wechat;

use App\Http\Controllers\Controller;
use App\Models\Child;
use App\Models\assessments;
use Illuminate\Support\Facades\Log;
use Illuminate\Http\Request;
use EasyWeChat\Factory;

class HistoryController extends BaseController
{
    public function index(Request $request, $id)
    {

        $child = Child::find($id);

        if (empty($child)) {
            return $this->json([], config('apicode.noRecord.msg'), config('apicode.noRecord.code'));
        }

        //判断用户是否拥有管理学生的权限
        $user = $this->getUser();

        if (!$user->can('allow', $child)) {
            return $this->json([], config('apicode.cannotManage.msg'), config('apicode.cannotManage.code'));
        }

        $history = History::with('game')->where("child_id", $id)
            ->simplePaginate(config('site.other.paginate'))->toArray();


        return $this->json(['list'=>$history,'child'=>$child]);
    }
    
    public function view($id){
    
        $history = History::with("game")->with("device")->find($id);

        if (empty($history)) {
            return $this->json([], config('apicode.noRecord.msg'), config('apicode.noRecord.code'));
        }
        
        //判断用户是否拥有管理训练记录的权限
        $user = $this->getUser();

        if (!$user->can('allow', $history->child)) {
            return $this->json([], config('apicode.cannotManage.msg'), config('apicode.cannotManage.code'));
        }
        $history->with("game")->with("device");
        return $this->json(['history' => $history]);        
    }
}
